//
//  RJHomeBottomView.m
//  AirQuality
//
//  Created by Po on 15/3/11.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJHomeBottomView.h"
#import "RJAQIScattergramView.h"
#import "HomeConnect.h"
#import "RJHomeTopSectionView.h"

@interface RJHomeBottomView () <RJHomeTopSectionViewDelegate, RJHomeBottomFatherViewMoveDelegate>

@property (retain, nonatomic) RJHomeBottomFatherView * currentBottomView;           //当前展示视图
@property (retain, nonatomic) RJHomeTopSectionView * typeView;                      //显示类型控件

@property (assign, nonatomic) CGFloat bottomViewHeight;
@property (assign, nonatomic) NSUInteger currentViewCount;                          //当前视图编号
@property (assign, nonatomic) BOOL userControl;                                     //用户交互
@property (assign, nonatomic) BOOL isLeft;

- (void)initData;
- (void)initInterface;
@end

@implementation RJHomeBottomView

- (instancetype)initWithFrame:(CGRect)frame vc:(UIViewController *)vc
{
    self = [super initWithFrame:frame];
    if (self) {
        self.vc = vc;
        [self initData];
        [self initInterface];
    }
    return self;
}

- (void)initData
{
    _userControl = NO;
    _isLeft = NO;
    _currentViewCount = 0;
    _bottomViewHeight = CGRectGetHeight(self.frame) - 20;
}

- (void)initInterface
{
    _currentBottomView = [self getNowBottomViewWithNum:_currentViewCount];
    _currentBottomView.vc = _vc;
    [_currentBottomView setFrame:CGRectMake(0, 40, ScreenWidth, _bottomViewHeight)];
    [self addSubview:_currentBottomView];
    [self addSubview:[self getTypeView]];
}

- (RJHomeTopSectionView *)getTypeView
{
    if (!_typeView) {
        _typeView = [[RJHomeTopSectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40) titles:@[@"全省AQI分布",@"实时联网"]];
        [_typeView setBackgroundColor:GetRGBColor(246, 250, 251, 1)];
        [_typeView setDelegate:self];
        [_typeView.layer setShadowColor:[UIColor grayColor].CGColor];
        [_typeView.layer setShadowOffset:CGSizeMake(0, 0.5)];
        [_typeView.layer setShadowOpacity:0.5];
    }
    return _typeView;
}

#pragma mark - function
- (void)freeData
{
    [_currentBottomView freeData];
}

/**更改指定视图*/
- (void)changeNowBottomViewWithNum:(NSUInteger)num
{
    //加载新视图
    RJHomeBottomFatherView * view = [self getNowBottomViewWithNum:num];
    [self addSubview:view];
    [view setFrame:CGRectMake(
                              _isLeft ? - ScreenWidth : ScreenWidth,
                              40,
                              ScreenWidth,
                              _bottomViewHeight
                              )];
    UIView * removeView = _currentBottomView;
    
    //移除当前视图
    [UIView animateWithDuration:0.5f animations:^{
        [removeView setFrame:CGRectMake(
                                                _isLeft ? ScreenWidth : -ScreenWidth,
                                                40,
                                                ScreenWidth,
                                                _bottomViewHeight)
         ];
    } completion:^(BOOL finished) {
        if (_userControl) {
            [removeView removeFromSuperview];
            
            _userControl = NO;
        }
    }];
    
    //显示新视图
    [UIView animateWithDuration:0.5f animations:^{
        [view showLoadingView];
        [view setFrame:CGRectMake(0, 40, ScreenWidth, _bottomViewHeight)];
    } completion:^(BOOL finished) {
        [_currentBottomView freeData];
        _currentBottomView = view;
        _currentViewCount = num;
    }];
}

/*获取视图**/
- (RJHomeBottomFatherView *)getNowBottomViewWithNum:(NSUInteger)num
{
    CGRect rect = self.bounds;
    RJHomeBottomFatherView * view;
    switch (num) {
        case 0:
            view = [[RJAQIScattergramView alloc] initWithFrame:rect vc:self.vc];
            break;
        case 1:
            view = [[HomeConnect alloc] initWithFrame:rect vc:self.vc];
            break;
        default:
            break;
    }
    view.vc = _vc;
    [view setMoveDelegate:self];
    return view;
}

#pragma mark - RJHomeTypeDelegate
- (BOOL)HomeTopSectionView:(RJHomeTopSectionView *)sectionView didSelectCount:(NSUInteger)count isLeft:(BOOL)isLeft
{
    if (!_userControl) {
        _isLeft = isLeft;
        _userControl = YES;
        [self changeNowBottomViewWithNum:count];
        return YES;
    }
    return NO;
}

#pragma mark - RJHomeBottomFatherViewMoveDelegate
- (void)touchMove:(BOOL)isLeft
{
    NSInteger page = isLeft ? _currentViewCount + 1 : _currentViewCount - 1;
    if (page < 0) {
        page = 1;
    }
    if (page > 1) {
        page = 0;
    }
    [_typeView setSelectCount:page isLeft:!isLeft];
    
}

@end
